//
//  BELCommand.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/15.
//  Copyright © 2016年 Fine. All rights reserved.
//

/*资料
 ios中将NSstring字符串转换成char类型
 NSString *string = [NSString stringWithFormat:@"5D"];
 const char *result = [string UTF8String];
 
 将char类型字符转成十进制类型，然后需要什么样的进制类型都可以直接转换了
 unsigned long num = strtoul(result, 0, 16);
 NSLog(@"%lu",num);
 NSLog(@"%0lx",num);
 NSLog(@"%0lX",num);
 %0x----是保存成十六进制的（字母是小写）
 %0X----是保存成十六进制的（字母是大写）
 
 
 IOS 十进制转十六进制
 NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",整形参数]];
 
 // 将6位16进制字符串，每两位一组截取并转换为10进制
 NSString *hexColor = @"ad9975";
 long redColor = strtoul([[hexColor substringWithRange:NSMakeRange(0, 2)] UTF8String], 0, 16);
 long greenColor = strtoul([[hexColor substringWithRange:NSMakeRange(2, 2)] UTF8String], 0, 16);
 long blueColor = strtoul([[hexColor substringWithRange:NSMakeRange(4, 2)] UTF8String], 0, 16);
 
 NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
 NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
 NSLog(@"time_strs = %@",timeSp);
 int num3 =[time_strs[0] intValue];
 int num4 =[time_strs[1] intValue];
 int num5 =[time_strs[2] intValue];
 int num6 =[time_strs[3] intValue];
 int num7 =[time_strs[4] intValue];
 //后三位90-11-5是把CMD_HEAD CMD_LENGHT CMD_SORT 转成的10进制
 int num8 = num3 + num4 +num5 +num6 +num7 +90+11+5;
 NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",num3]];
 //一个字节为8位二进制位
 Byte   CMD_HEAD = 0x5A;
 Byte   CMD_LENGHT = 0x0B;
 Byte   CMD_SORT = 0x05;
 Byte byte4[] = {CMD_HEAD,CMD_LENGHT,CMD_SORT,num3,num4,num5,num6,num7,num8,0,0};
 NSData *data25 = [NSData dataWithBytes:byte4 length:sizeof(byte4)];
 
 */

#import "BELCommand.h"

@implementation BELCommand

- (void)hexToBytes:(NSString *)hexString byte:(Byte[])bytes{
    int j=0;
    for(int i= 0;i<[hexString length];i++)
    {
        int int_ch;  /// 两位16进制数转化后的10进制数
        int int_ch1;
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        
        int int_ch2=0;
        if ([hexString length]%2 == 0 && i != [hexString length]) {
            unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
            if(hex_char2 >= '0' && hex_char2 <='9')
                int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
            else if(hex_char1 >= 'A' && hex_char1 <='F')
                int_ch2 = hex_char2-55; //// A 的Ascll - 65
            else
                int_ch2 = hex_char2-87; //// a 的Ascll - 97
            int_ch = int_ch2+int_ch1;
        }else{
            int_ch = int_ch1+int_ch2;
        }
        NSLog(@"int_ch=%d",int_ch);
        bytes[j] = int_ch;  ///将转化后的数放入Byte数组里
        j++;
    }
    //return bytes;
}

//设置步数
- (NSData *)setMovement:(NSString *)stepCount ccid:(NSString *)ccid{
    //十进制转十六进制
    NSString *hexStringCCID = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",[ccid intValue]]];
    NSString *hexStringStep = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",[stepCount intValue]]];
    //帧头
    Byte   CMD_HEAD1 = 0x01;
    //ccid
    Byte   CMD_CCID2 = 0x00;
    Byte   CMD_CCID3 = 0x00;
    Byte bytes[2];
    [self hexToBytes:hexStringCCID byte:bytes];
    //帧总数
    Byte   CMD_TOTAL6 = 0x00;
    Byte   CMD_TOTAL7 = 0x01;
    //帧序号
    Byte   CMD_SERIAL8 = 0x00;
    Byte   CMD_SERIAL9 = 0x00;
    //字节长
    Byte   CMD_LENGHT10 = 0x14;
    //命令大类
    Byte   CMD_TYPE11 = 0x00;
    //命令小类
    Byte   CMD_TYPE12 = 0x00;
    //命令字
    Byte   CMD_WORD = 0x00;
    Byte bytesStep[4] = {CMD_WORD,CMD_WORD,CMD_WORD,CMD_WORD};
    if (hexStringStep.length % 2 == 0) {
        [self hexToBytes:hexStringStep byte:bytesStep];
    }else{
        [self hexToBytes:[NSString stringWithFormat:@"0%@",hexStringStep] byte:bytesStep];
    }
    //效验和
    int total = CMD_HEAD1+CMD_CCID2+CMD_CCID3+bytes[0]+bytes[1]+CMD_TOTAL6+CMD_TOTAL7+CMD_SERIAL8+CMD_SERIAL9+CMD_LENGHT10+CMD_TYPE11+CMD_TYPE12+bytesStep[3]+bytesStep[2]+bytesStep[1]+bytesStep[0];
    NSString *hexStringEffectiveness = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",total]];
    Byte bytesEffectiveness[4] = {CMD_WORD,CMD_WORD,CMD_WORD,CMD_WORD};
    if (hexStringEffectiveness.length % 2 == 0) {
        [self hexToBytes:hexStringEffectiveness byte:bytesEffectiveness];
    }else{
        [self hexToBytes:[NSString stringWithFormat:@"0%@",hexStringEffectiveness] byte:bytesEffectiveness];
    }
    //最终命令
    Byte byte4[] = {CMD_HEAD1,CMD_CCID2,CMD_CCID3,bytes[0],bytes[1],CMD_TOTAL6,CMD_TOTAL7,CMD_SERIAL8,CMD_SERIAL9,CMD_LENGHT10,CMD_TYPE11,CMD_TYPE12,bytesStep[3],bytesStep[2],bytesStep[1],bytesStep[0],bytesEffectiveness[3],bytesEffectiveness[2],bytesEffectiveness[1],bytesEffectiveness[0]};
    NSData *data25 = [NSData dataWithBytes:byte4 length:sizeof(byte4)];
    return data25;
}



//将十进制转化为十六进制
- (NSString *)ToHex:(uint16_t)tmpid{
    NSString *nLetterValue;
    NSString *str =@"";
    uint16_t ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig){
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:
                nLetterValue = [NSString stringWithFormat:@"%u",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
    }
    return str;
}
@end
